package main;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.dom4j.DocumentException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cputils.CPDataService;
import cputils.DataItem;
import cputils.XmlUtils;

public class Main {
	
	CPDataService cpService = new CPDataService();
	XmlUtils utils = new XmlUtils();
	private Text logText;
	private Text beginText;
	private Text endText;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Main window = new Main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		Shell shlMyApp = new Shell();
		shlMyApp.setSize(587, 409);
		shlMyApp.setText("My App");
		shlMyApp.setLocation(display.getClientArea().width/2-shlMyApp.getSize().x/2, display.getClientArea().height/2-shlMyApp.getSize().y/2);
		final Label lblErrMsgLabel = new Label(shlMyApp, SWT.NONE);
		lblErrMsgLabel.setBounds(429, 13, 132, 17);
		logText = new Text(shlMyApp, SWT.WRAP|SWT.V_SCROLL);
		logText.setBounds(10, 79, 551, 282);
		
		final Combo combo = new Combo(shlMyApp, SWT.NONE);
		combo.setItems(new String[] {"101", "201"});
		combo.setBounds(47, 10, 70, 25);
		combo.select(0);
		combo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				switch (Integer.parseInt(combo.getText())) {
				case 101:
					beginText.setText("2014001");
					break;
				case 201:
					beginText.setText("14001");
					break;
				default:
					break;
				}
			}
		});
		
		final Button btnUpdateData = new Button(shlMyApp, SWT.NONE);
		btnUpdateData.setBounds(10, 42, 55, 27);
		btnUpdateData.setText("Update");
		btnUpdateData.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnUpdateData.setEnabled(false);
				try {
					if ("".equals(endText.getText())) {
						throw new IOException("please input end value...");
					}
					if ("101".equals(combo.getText())) {
						utils.importSSQData(0, 2014100);
					}else if ("201".equals(combo.getText())) {
						utils.importDLTData(0, 14100);
					}else {
						logText.setText("select type...");
					}
				} catch (IOException e1) {
					logText.setText(e1.getMessage());
					popErr(e1.getMessage());
				} catch (DocumentException e2) {
					logText.setText(e2.getMessage());
					e2.printStackTrace();
				}finally{
					btnUpdateData.setEnabled(true);
				}
			}
		});
		
		final Button btnShowDataButton = new Button(shlMyApp, SWT.NONE);
		btnShowDataButton.setBounds(71, 41, 42, 27);
		btnShowDataButton.setText("Show");
		btnShowDataButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnShowDataButton.setEnabled(false);
				try {
					String type = "";
					if ("".equals(combo.getText())){
						lblErrMsgLabel.setText("select type...");
					}else {
						if ("101".equals(combo.getText())) {
							type = "ssq";
						}else if ("201".equals(combo.getText())) {
							type = "dlt";
						}
						List<DataItem> list = utils.list(type);
						logText.setText("");
						for (DataItem item : list) {
							logText.append(item.getId()+":"+item.getOpencode()+"\n");
						}
						logText.append("finished..."+list.size());
					}
				} catch (IOException e1) {
					logText.setText(e1.getMessage());
					popErr(e1.getMessage());
				} catch (DocumentException e1) {
					logText.setText(e1.getMessage());
					e1.printStackTrace();
				}finally{
					btnShowDataButton.setEnabled(true);
				}
			}
		});
		
		
		Label lblNewLabel = new Label(shlMyApp, SWT.NONE);
		lblNewLabel.setBounds(10, 13, 31, 17);
		lblNewLabel.setText("Type:");
		
		final Button btnStatDataButton = new Button(shlMyApp, SWT.NONE);
		btnStatDataButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnStatDataButton.setEnabled(false);
				try {
					if ("".equals(combo.getText())){
						lblErrMsgLabel.setText("select type...");
					}else {
						Map<String, Map<Integer, Integer>> map = null;
						if ("101".equals(combo.getText())) {
							map = utils.tjSSQ(true);
						}else if ("201".equals(combo.getText())) {
							map = utils.tjDLT(true);
						}
						logText.setText("");
						if (!map.isEmpty()) {
							int i = 1;
							Map<Integer, Integer> beforeMap = map.get("b");
							Map<Integer, Integer> afterMap = map.get("a");
							logText.append("before:\n");
							for (Entry<Integer, Integer> entry : beforeMap.entrySet()) {
								logText.append(entry.toString()+",");
								if (i%5==0) {
									logText.append("\n");
								}
								i++;
							}
							if ("101".equals(combo.getText())) {
								logText.append("\n");
							}
							logText.append("after:\n");
							for (Entry<Integer, Integer> entry : afterMap.entrySet()) {
								logText.append(entry.toString()+",");
							}
						}else {
							logText.append("err...");
						}
					}
				} catch (IOException e1) {
					logText.setText(e1.getMessage());
					popErr(e1.getMessage());
				} catch (DocumentException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}finally{
					btnStatDataButton.setEnabled(true);
				}
			}
		});
		btnStatDataButton.setBounds(119, 41, 34, 27);
		btnStatDataButton.setText("Stat");
		
		Button btnMakeDataButton = new Button(shlMyApp, SWT.NONE);
		btnMakeDataButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		btnMakeDataButton.setBounds(159, 42, 42, 27);
		btnMakeDataButton.setText("Make");
		
		Label lblBegin = new Label(shlMyApp, SWT.NONE);
		lblBegin.setBounds(128, 13, 42, 17);
		lblBegin.setText("Begin:");
		
		beginText = new Text(shlMyApp, SWT.BORDER);
		beginText.setText("2014001");
		beginText.setBounds(167, 10, 73, 23);
		
		Label lblEnd = new Label(shlMyApp, SWT.NONE);
		lblEnd.setBounds(249, 13, 31, 17);
		lblEnd.setText("End:");
		
		endText = new Text(shlMyApp, SWT.BORDER);
		endText.setBounds(277, 10, 73, 23);
		
		Button btnNewButton = new Button(shlMyApp, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnNewButton.setBounds(297, 42, 53, 27);
		btnNewButton.setText("Update");
		
		Button btnNewButton_1 = new Button(shlMyApp, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnNewButton_1.setBounds(356, 42, 42, 27);
		btnNewButton_1.setText("Show");
		
		Button btnFinal = new Button(shlMyApp, SWT.NONE);
		btnFinal.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnFinal.setBounds(229, 42, 48, 27);
		btnFinal.setText("Final");
		
		Button btnStat = new Button(shlMyApp, SWT.NONE);
		btnStat.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnStat.setBounds(402, 42, 42, 27);
		btnStat.setText("Stat");
		
		Button btnMake = new Button(shlMyApp, SWT.NONE);
		btnMake.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnMake.setBounds(450, 42, 42, 27);
		btnMake.setText("Make");
		shlMyApp.setTabList(new Control[]{btnUpdateData, btnShowDataButton, btnStatDataButton, btnMakeDataButton, beginText, endText, btnNewButton, btnNewButton_1, btnFinal, btnStat, btnMake, combo, logText});
		
		shlMyApp.open();
		shlMyApp.layout();
		while (!shlMyApp.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	private void popErr(String text){
		MessageBox msgBox = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_ERROR);  
	    msgBox.setMessage(text);  
	    msgBox.open();
	}
}
